mod common;
use crate::common::*;
use crate::station::ck_station;
use serde_json::json;

#[tokio::test]
async fn test_station_1() {
    do_ret(
        pos!(),
        json!({
            "line": "1",
            "v": {
                "sn": "",
                "name": "name_M010",
            }
        }),
        "station",
        "add",
        e_miss("sn"),
    )
    .await;

    do_ret(
        pos!(),
        json!({
            "line": "1",
        }),
        "station",
        "list",
        e_node(),
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "v": {
                "sn": "M010",
                "name": "name_M010",
            }
        }),
        "station",
        "add",
    )
    .await;

    ck_station(
        pos!(),
        json!({
            "all": ["M010"],
            "M010": {"name": "name_M010", "sn": "M010"}
        }),
    )
    .await;

    ok_ret(
        pos!(),
        json!({
            "line": "1",
        }),
        "station",
        "list",
        json!([{"name": "name_M010", "sn": "M010"}]),
    )
    .await;

    ok_ret(
        pos!(),
        json!({
            "line": "1",
            "k": "M010",
        }),
        "station",
        "get",
        json!({"name": "name_M010", "sn": "M010"}),
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "k": "M010",
        }),
        "station",
        "rm",
    )
    .await;

    do_ret(
        pos!(),
        json!({
            "line": "1",
        }),
        "station",
        "list",
        e_node(),
    )
    .await;

    do_ret(
        pos!(),
        json!({
            "line": "1",
            "k": "M010",
        }),
        "station",
        "get",
        e_node(),
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "k": "M010",
        }),
        "station",
        "rm",
    )
    .await;

    ck_station(pos!(), json!({})).await;
}

#[tokio::test]
async fn test_station_2() {
    ok(
        pos!(),
        json!({
            "line": "1",
            "v": {
                "sn": "M010",
                "name": "name_M010",
            }
        }),
        "station",
        "add",
    )
    .await;

    ck_station(
        pos!(),
        json!({
            "all": ["M010"],
            "M010": {"name": "name_M010", "sn": "M010"}
        }),
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "v": {
                "sn": "M020",
                "name": "name_M020",
            }
        }),
        "station",
        "add",
    )
    .await;

    ck_station(
        pos!(),
        json!({
            "all": ["M010", "M020"],
            "M010": {"name": "name_M010", "sn": "M010"},
            "M020": {"name": "name_M020", "sn": "M020"},
        }),
    )
    .await;

    ok_ret(
        pos!(),
        json!({
            "line": "1",
        }),
        "station",
        "list",
        json!([
                {"sn": "M010", "name": "name_M010"},
                {"sn": "M020", "name": "name_M020"},
        ]),
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "v": [{
                "sn": "M010",
                "name": "n_M010",
            }]
        }),
        "station",
        "md",
    )
    .await;

    ck_station(
        pos!(),
        json!({
            "all": ["M010", "M020"],
            "M010": {"name": "n_M010", "sn": "M010"},
            "M020": {"name": "name_M020", "sn": "M020"},
        }),
    )
    .await;

    ok_ret(
        pos!(),
        json!({
            "line": "1",
        }),
        "station",
        "list",
        json!([
                {"sn": "M010", "name": "n_M010"},
                {"sn": "M020", "name": "name_M020"},
        ]),
    )
    .await;

    ok_ret(
        pos!(),
        json!({
            "line": "1",
            "k": "M010",
        }),
        "station",
        "get",
        json!({"sn": "M010", "name": "n_M010"}),
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "k": "M010",
        }),
        "station",
        "rm",
    )
    .await;

    ck_station(
        pos!(),
        json!({
            "all": ["M020"],
            "M020": {"name": "name_M020", "sn": "M020"},
        }),
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "k": "M020",
        }),
        "station",
        "rm",
    )
    .await;

    ck_station(pos!(), json!({})).await;

    do_ret(
        pos!(),
        json!({
            "line": "1",
        }),
        "station",
        "list",
        e_node(),
    )
    .await;
}

#[tokio::test]
async fn test_station_3() {
    ok(
        pos!(),
        json!({
            "line": "1",
            "v": {
                "sn": "M010",
                "name": "name_M010",
            }
        }),
        "station",
        "add",
    )
    .await;

    ck_station(
        pos!(),
        json!({
            "all": ["M010"],
            "M010": {"name": "name_M010", "sn": "M010"}
        }),
    )
    .await;

    do_ret(
        pos!(),
        json!({
            "line": "1",
            "k": "M010",
        }),
        "station",
        "getpsn",
        e_empty("no data"),
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "k": "M010",
            "sn": "psn1",
        }),
        "station",
        "rmpsn",
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "k": "M010",
            "v": ["psn1", "psn2"]
        }),
        "station",
        "addpsn",
    )
    .await;

    ok_ret(
        pos!(),
        json!({
            "line": "1",
            "k": "M010",
        }),
        "station",
        "getpsn",
        json!({"psn": ["psn1", "psn2"]}),
    )
    .await;

    ck_station(
        pos!(),
        json!({
            "all": ["M010"],
            "M010": {"name": "name_M010", "sn": "M010"},
            "psn1": ["psn1", "psn2"],
        }),
    )
    .await;

    ok_ret(
        pos!(),
        json!({
            "line": "1",
            "k": "M010",
        }),
        "station",
        "getpsn",
        json!({"psn": ["psn1", "psn2"]}),
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "k": "M010",
            "sn": "psn1",
        }),
        "station",
        "rmpsn",
    )
    .await;

    ck_station(
        pos!(),
        json!({
            "all": ["M010"],
            "M010": {"name": "name_M010", "sn": "M010"},
            "psn1": ["psn2"],
        }),
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "k": "M010",
            "sn": "psn2",
        }),
        "station",
        "rmpsn",
    )
    .await;

    ck_station(
        pos!(),
        json!({
            "all": ["M010"],
            "M010": {"name": "name_M010", "sn": "M010"},
        }),
    )
    .await;

    do_ret(
        pos!(),
        json!({
            "line": "1",
            "k": "M010",
        }),
        "station",
        "getpsn",
        e_empty("no data"),
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "k": "M010",
            "v": ["psn1"]
        }),
        "station",
        "addpsn",
    )
    .await;

    ck_station(
        pos!(),
        json!({
            "all": ["M010"],
            "M010": {"name": "name_M010", "sn": "M010"},
            "psn1": ["psn1"],
        }),
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "k": "M010",
            "rework": true,
            "v": ["psn2"]
        }),
        "station",
        "addpsn",
    )
    .await;

    ck_station(
        pos!(),
        json!({
            "all": ["M010"],
            "M010": {"name": "name_M010", "sn": "M010"},
            "psn1": ["psn1"],
            "rework1": ["psn2"],
        }),
    )
    .await;

    ok_ret(
        pos!(),
        json!({
            "line": "1",
            "k": "M010",
        }),
        "station",
        "getpsn",
        json!({"psn": ["psn1"], "rework": ["psn2"]}),
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "k": "M010",
            "sn": "psn1",
        }),
        "station",
        "rmpsn",
    )
    .await;

    ck_station(
        pos!(),
        json!({
            "all": ["M010"],
            "M010": {"name": "name_M010", "sn": "M010"},
            "rework1": ["psn2"],
        }),
    )
    .await;

    ok_ret(
        pos!(),
        json!({
            "line": "1",
            "k": "M010",
        }),
        "station",
        "getpsn",
        json!({"rework": ["psn2"]}),
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "k": "M010",
            "rework": true,
            "sn": "psn2",
        }),
        "station",
        "rmpsn",
    )
    .await;

    do_ret(
        pos!(),
        json!({
            "line": "1",
            "k": "M010",
        }),
        "station",
        "getpsn",
        e_empty("no data"),
    )
    .await;

    ck_station(
        pos!(),
        json!({
            "all": ["M010"],
            "M010": {"name": "name_M010", "sn": "M010"},
        }),
    )
    .await;
}

#[tokio::test]
async fn test_station_4() {
    ok(
        pos!(),
        json!({
            "line": "1",
            "v": {
                "sn": "M010",
                "name": "name_M010",
            }
        }),
        "station",
        "add",
    )
    .await;

    ck_station(
        pos!(),
        json!({
            "all": ["M010"],
            "M010": {"name": "name_M010", "sn": "M010"},
        }),
    )
    .await;

    do_ret(
        pos!(),
        json!({
            "line": "1",
            "k": "M010",
        }),
        "station",
        "getuser",
        e_node(),
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "k": "M010",
            "sn": "1001",
        }),
        "station",
        "rmuser",
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "k": "M010",
            "v": ["1001", "1002"]
        }),
        "station",
        "adduser",
    )
    .await;

    ok_ret(
        pos!(),
        json!({
            "line": "1",
            "k": "M010",
        }),
        "station",
        "getuser",
        json!(["1001", "1002"]),
    )
    .await;

    ck_station(
        pos!(),
        json!({
            "all": ["M010"],
            "M010": {"name": "name_M010", "sn": "M010"},
            "user1": ["1001", "1002"]
        }),
    )
    .await;

    ok_ret(
        pos!(),
        json!({
            "line": "1",
            "k": "M010",
        }),
        "station",
        "getuser",
        json!(["1001", "1002"]),
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "k": "M010",
            "sn": "1001",
        }),
        "station",
        "rmuser",
    )
    .await;

    ck_station(
        pos!(),
        json!({
            "all": ["M010"],
            "M010": {"name": "name_M010", "sn": "M010"},
            "user1": ["1002"]
        }),
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "k": "M010",
            "sn": "1002",
        }),
        "station",
        "rmuser",
    )
    .await;

    ck_station(
        pos!(),
        json!({
            "all": ["M010"],
            "M010": {"name": "name_M010", "sn": "M010"},
        }),
    )
    .await;

    do_ret(
        pos!(),
        json!({
            "line": "1",
            "k": "M010",
        }),
        "station",
        "getuser",
        e_node(),
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "k": "M010",
            "v": ["1001"]
        }),
        "station",
        "adduser",
    )
    .await;

    ok_ret(
        pos!(),
        json!({
            "line": "1",
            "k": "M010",
        }),
        "station",
        "getuser",
        json!(["1001"]),
    )
    .await;

    ck_station(
        pos!(),
        json!({
            "all": ["M010"],
            "M010": {"name": "name_M010", "sn": "M010"},
            "user1": ["1001"]
        }),
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "k": "M010",
            "sn": "1001",
        }),
        "station",
        "rmuser",
    )
    .await;

    ck_station(
        pos!(),
        json!({
            "all": ["M010"],
            "M010": {"name": "name_M010", "sn": "M010"},
        }),
    )
    .await;

    do_ret(
        pos!(),
        json!({
            "line": "1",
            "k": "M010",
        }),
        "station",
        "getuser",
        e_node(),
    )
    .await;
}

#[tokio::test]
async fn test_station_5() {
    ok(
        pos!(),
        json!({
            "line": "1",
            "v": {
                "sn": "M010",
                "name": "name_M010",
            }
        }),
        "station",
        "add",
    )
    .await;

    ck_station(
        pos!(),
        json!({
            "all": ["M010"],
            "M010": {"name": "name_M010", "sn": "M010"},
        }),
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "k": "M010",
            "v": ["psn1", "psn2"]
        }),
        "station",
        "addpsn",
    )
    .await;

    ok_ret(
        pos!(),
        json!({
            "line": "1",
            "k": "M010",
        }),
        "station",
        "getpsn",
        json!({"psn": ["psn1", "psn2"]}),
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "k": "M010",
            "v": ["1001", "1002"]
        }),
        "station",
        "adduser",
    )
    .await;

    ok_ret(
        pos!(),
        json!({
            "line": "1",
            "k": "M010",
        }),
        "station",
        "getuser",
        json!(["1001", "1002"]),
    )
    .await;

    ck_station(
        pos!(),
        json!({
            "all": ["M010"],
            "M010": {"name": "name_M010", "sn": "M010"},
            "psn1": ["psn1", "psn2"],
            "user1": ["1001", "1002"]
        }),
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "k": "M010",
            "v": ["1001", "1002"]
        }),
        "station",
        "adduser",
    )
    .await;

    ck_station(
        pos!(),
        json!({
            "all": ["M010"],
            "M010": {"name": "name_M010", "sn": "M010"},
            "psn1": ["psn1", "psn2"],
            "user1": ["1001", "1002"]
        }),
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "k": "M010",
            "rework": true,
            "v": ["psn2"]
        }),
        "station",
        "addpsn",
    )
    .await;

    ck_station(
        pos!(),
        json!({
            "all": ["M010"],
            "M010": {"name": "name_M010", "sn": "M010"},
            "psn1": ["psn1", "psn2"],
            "rework1": ["psn2"],
            "user1": ["1001", "1002"]
        }),
    )
    .await;

    ok_ret(
        pos!(),
        json!({
            "line": "1",
            "k": "M010",
        }),
        "station",
        "getpsn",
        json!({"psn": ["psn1", "psn2"], "rework": ["psn2"]}),
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "k": "M010",
        }),
        "station",
        "rm",
    )
    .await;

    ck_station(pos!(), json!({})).await;
}
